//Component1.jsx
import React from 'react';

class Header extends React.Component {
  constructor(props) {
    super(props);
    this.state={
      time:''
    }
    this.updateTime = this.updateTime.bind(this);
  }

  componentDidMount() {
    this.timerID = setInterval(
      () => this.updateTime(),
      1000
    );
  }

  componentWillUnmount() {
    clearInterval(this.timerID);
  }

  updateTime() {
    let now = new Date();
    let year = now.getFullYear();
    let month = now.getMonth() + 1<10?'0'+(now.getMonth() + 1):(now.getMonth() + 1)
    let day = now.getDate()<10?'0'+now.getDate():now.getDate()
    let hour = now.getHours()<10?'0'+now.getHours():now.getHours()
    let minute = now.getMinutes()<10?'0'+now.getMinutes():now.getMinutes()
    let second = now.getSeconds()<10?'0'+now.getSeconds():now.getSeconds()
    this.setState({
      // time: now.getFullYear() + '-' + (now.getMonth() + 1)>9?'0'+(now.getMonth() + 1):(now.getMonth() + 1)+ '-' + now.getDate() + ' ' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds()
      time: year + '-' + month+ '-' + day + ' ' + hour + ':' + minute + ':' + second
    });
  }
  render() {
    return (
      <div className="header">
          <div className="time">
            {this.state.time}
          </div>
      </div>
    )
  }
}

//导出组件
export default Header;
